<template>
  <div class="app-container">
    <el-card class="box-card" shadow="hover" style="border: #ffffff">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="客户号码" prop="cdrCustomerNumber">
          <el-input
            v-model="queryParams.cdrCustomerNumber"
            placeholder="请输入客户号码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="选择时间">
            <el-date-picker
              v-model="daterangeCreateTime"
              size="small"
              style="width: 215px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
            ></el-date-picker>
          </el-form-item>
        <div class="header_btn">
          <el-button size="small" type="primary" @click="handleQuery">查询</el-button>
          <el-button size="small" @click="resetQuery">重置</el-button>
        </div>
      </el-form>
      <el-card class="box-card" shadow="hover">
        <el-table border v-loading="loading" :data="webcallList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="企业ID" align="center" prop="cdrEnterpriseId" />
          <el-table-column label="通话标示" align="center" width="180" prop="cdrMainUniqueId" />
          <el-table-column label="客户号码" align="center"  width="150" prop="mobileHidden" />
          <el-table-column label="所属区号" align="center" prop="cdrCustomerAreaCode" />
          <el-table-column label="所属省市" width="150">
            <template slot-scope="scope">
              {{scope.row.cdrCustomerProvince|formatVal}}/{{scope.row.cdrCustomerCity|formatVal}}
            </template>
          </el-table-column>
          <el-table-column label="号码类型" :formatter="crmNumberType" />
          <el-table-column label="呼叫状态" :formatter="cdrStatusFormat" width="140"></el-table-column>
          <el-table-column label="呼叫类型" :formatter="cdrCallTypeFormat" width="120"></el-table-column>
          <el-table-column label="部门名称" align="center" prop="deptId" :formatter="deptIdFormat"/>
          <el-table-column label="用户名称" align="center" prop="userId" :formatter="userIdFormat"/>
          <el-table-column label="呼叫开始时间" align="center" width="150"  prop="cdrStartTime" >
            <template slot-scope="scope">
              {{scope.row.cdrStartTime*1000|dataFormat}}
            </template>
          </el-table-column>
          <el-table-column label="客户接听/挂机时间" align="center"  width="150" prop="cdrAnswerTime" >
            <template slot-scope="scope">
              {{scope.row.cdrAnswerTime*1000|dataFormat}}<br/>
              {{scope.row.cdrEndTime*1000|dataFormat}}
            </template>
          </el-table-column>

          <el-table-column label="开始时间" align="center" prop="createTime" width="150">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="操作" fixed="right" width="90">
             <template slot-scope="scope">
               <el-button size="mini" type="text" @click="openCallDetailsDialog(scope.row.id)" icon="el-icon-document">详细</el-button>
               <br />
             </template>
           </el-table-column> -->
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-card>
    </el-card>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:webcall:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:webcall:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:webcall:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->



    <!-- 添加或修改webCall语音通知对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业ID" prop="cdrEnterpriseId">
          <el-input v-model="form.cdrEnterpriseId" placeholder="请输入企业ID" />
        </el-form-item>
        <el-form-item label="座席工号" prop="cdrCalleeCno">
          <el-input v-model="form.cdrCalleeCno" placeholder="请输入座席工号" />
        </el-form-item>
        <el-form-item label="分机号" prop="cdrCalleeExten">
          <el-input v-model="form.cdrCalleeExten" placeholder="请输入分机号" />
        </el-form-item>
        <el-form-item label="通话标示" prop="cdrMainUniqueId">
          <el-input v-model="form.cdrMainUniqueId" placeholder="请输入通话标示" />
        </el-form-item>
        <el-form-item label="客户号码" prop="cdrCustomerNumber">
          <el-input v-model="form.cdrCustomerNumber" placeholder="请输入客户号码" />
        </el-form-item>
        <el-form-item label="虚拟小号" prop="cdrXNumber">
          <el-input v-model="form.cdrXNumber" placeholder="请输入虚拟小号" />
        </el-form-item>
        <el-form-item label="客户号码所属区号" prop="cdrCustomerAreaCode">
          <el-input v-model="form.cdrCustomerAreaCode" placeholder="请输入客户号码所属区号" />
        </el-form-item>
        <el-form-item label="客户号码所属城市" prop="cdrCustomerCity">
          <el-input v-model="form.cdrCustomerCity" placeholder="请输入客户号码所属城市" />
        </el-form-item>
        <el-form-item label="客户号码所属城市" prop="cdrCustomerProvince">
          <el-input v-model="form.cdrCustomerProvince" placeholder="请输入客户号码所属城市" />
        </el-form-item>
        <el-form-item label="呼叫开始时间" prop="cdrStartTime">
          <el-input v-model="form.cdrStartTime" placeholder="请输入呼叫开始时间" />
        </el-form-item>
        <el-form-item label="客户振铃时间" prop="calleeRingingTime">
          <el-input v-model="form.calleeRingingTime" placeholder="请输入客户振铃时间" />
        </el-form-item>
        <el-form-item label="webcall被叫侧(非座席)振铃时间" prop="detailRingingTime">
          <el-input v-model="form.detailRingingTime" placeholder="请输入webcall被叫侧(非座席)振铃时间" />
        </el-form-item>
        <el-form-item label="双方桥接时间" prop="cdrBridgeTime">
          <el-input v-model="form.cdrBridgeTime" placeholder="请输入双方桥接时间" />
        </el-form-item>
        <el-form-item label="客户接听时间" prop="cdrAnswerTime">
          <el-input v-model="form.cdrAnswerTime" placeholder="请输入客户接听时间" />
        </el-form-item>
        <el-form-item label="挂机时间" prop="cdrEndTime">
          <el-input v-model="form.cdrEndTime" placeholder="请输入挂机时间" />
        </el-form-item>
        <el-form-item label="通话请求id" prop="cdrRequestUniqueId">
          <el-input v-model="form.cdrRequestUniqueId" placeholder="请输入通话请求id" />
        </el-form-item>
        <el-form-item label="录音文件名称" prop="cdrRecordFile1">
          <el-input v-model="form.cdrRecordFile1" placeholder="请输入录音文件名称" />
        </el-form-item>
        <el-form-item label="挂机原因，取值说明：1000 主通道挂机; 1001 非主通道挂机;" prop="cdrEndReason">
          <el-input v-model="form.cdrEndReason" placeholder="请输入挂机原因，取值说明：1000 主通道挂机; 1001 非主通道挂机;" />
        </el-form-item>
        <el-form-item label="双方接听时长" prop="cdrEndTimeCdrBridgeTime">
          <el-input v-model="form.cdrEndTimeCdrBridgeTime" placeholder="请输入双方接听时长" />
        </el-form-item>
        <el-form-item label="座席绑定电话" prop="cdrCalleeNumber">
          <el-input v-model="form.cdrCalleeNumber" placeholder="请输入座席绑定电话" />
        </el-form-item>
        <el-form-item label="客户侧外显号码" prop="cdrClid">
          <el-input v-model="form.cdrClid" placeholder="请输入客户侧外显号码" />
        </el-form-item>
        <el-form-item label="webcall请求唯一标识" prop="webcallRequestUniqueId">
          <el-input v-model="form.webcallRequestUniqueId" placeholder="请输入webcall请求唯一标识" />
        </el-form-item>
        <el-form-item label="号码状态识别结果 710:忙 711:超时 712:拒接 713:空号 714:关机" prop="cdrDetailSipCause">
          <el-input v-model="form.cdrDetailSipCause" placeholder="请输入号码状态识别结果 710:忙 711:超时 712:拒接 713:空号 714:关机" />
        </el-form-item>
        <el-form-item label="客户id" prop="crmId">
          <el-input v-model="form.crmId" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item label="二级部门ID" prop="mchId">
          <el-input v-model="form.mchId" placeholder="请输入二级部门ID" />
        </el-form-item>
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="文件大小" prop="fileSize">
          <el-input v-model="form.fileSize" placeholder="请输入文件大小" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="手机号脱敏" prop="mobileHidden">
          <el-input v-model="form.mobileHidden" placeholder="请输入手机号脱敏" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWebcall, getWebcall, delWebcall, addWebcall, updateWebcall } from "@/api/call/webcall";
import  {formatDate} from "@/utils/index"
import { listDept } from '@/api/system/dept';
import { getUserList } from '@/api/system/user';
export default {
  name: "Webcall",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // webCall语音通知表格数据
      webcallList: [],
      // 部门列表
      deptList: [],
      //下拉框部门
      selectDept: [],

      // 用户列表
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cdrCalleeCno: null,
        cdrCustomerNumber: null,

      },
      //查询销售
      userParams:{
        deptId:null,
        userId:null,
        status:'0',
        delFlag:'0'
      },      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      daterangeCreateTime:[(new Date(new Date().getTime()-24*60*60*7000)).getFullYear() + "-" + ((new Date(new Date().getTime()-24*60*60*7000)).getMonth() + 1) + "-" + ((new Date(new Date().getTime()-24*60*60*7000)).getDate()),(new Date).getFullYear() + "-" + ((new Date).getMonth() + 1) + "-" + ((new Date).getDate()+1)],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            end.setTime(end.getTime() + 3600 * 1000 * 24 );
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            end.setTime(end.getTime() + 3600 * 1000 * 24 );
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            end.setTime(end.getTime() + 3600 * 1000 * 24 );
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  dicts: ["crm_call_status","crm_call_type","crm_number_type"],
  created() {
    this.getListDepts();

    this.getList();

    getUserList(this.userParams).then(response => {
      this.userList = response.data;
    });
  },
  filters:{
    formatVal(value){
      return value ? value : "-"
    },
    dataFormat(value){
      return formatDate(value) || "-"
    }
  },
  methods: {
    /** 查询webCall语音通知列表 */
    getList() {

      this.loading = true;
      this.queryParams.params = {};
      // this.queryParams.cdrCno = agentId;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["startTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["cdrEndTime"] = this.daterangeCreateTime[1];
      }
      listWebcall(this.queryParams).then(response => {
        this.webcallList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cdrEnterpriseId: null,
        cdrCalleeCno: null,
        cdrCalleeExten: null,
        cdrMainUniqueId: null,
        cdrCustomerNumber: null,
        cdrXNumber: null,
        cdrCustomerAreaCode: null,
        cdrCustomerCity: null,
        cdrCustomerProvince: null,
        cdrCustomerNumberType: null,
        cdrStatus: 0,
        cdrCallType: null,
        cdrStartTime: null,
        calleeRingingTime: null,
        detailRingingTime: null,
        cdrBridgeTime: null,
        cdrAnswerTime: null,
        cdrEndTime: null,
        cdrRequestUniqueId: null,
        cdrRecordFile1: null,
        cdrEndReason: null,
        cdrEndTimeCdrBridgeTime: null,
        cdrCalleeNumber: null,
        cdrClid: null,
        webcallRequestUniqueId: null,
        cdrDetailSipCause: null,
        createTime: null,
        crmId: null,
        mchId: null,
        deptId: null,
        fileSize: null,
        userId: null,
        mobileHidden: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加webCall语音通知";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWebcall(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改webCall语音通知";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWebcall(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWebcall(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除webCall语音通知编号为"' + ids + '"的数据项？').then(function() {
        return delWebcall(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/webcall/export', {
        ...this.queryParams
      }, `webcall_${new Date().getTime()}.xlsx`)
    },
    // 呼叫类型
    cdrCallTypeFormat(row, column){
      return this.selectDictLabel(this.dict.type.crm_call_type, row.cdrCallType ) || "-";
    },
     // 呼叫状态
    cdrStatusFormat(row, column){
     return this.selectDictLabel(this.dict.type.crm_call_status, row.cdrStatus ) || "-";
    },
    // 号码类型
    crmNumberType(row, column){
     return this.selectDictLabel(this.dict.type.crm_number_type, row.cdrCustomerNumberType ) || "-";
    },
      /** 获取部门 */
    getListDepts() {
      listDept().then(response => {
        this.deptList = response.data
      })
    },
    /** 部门 */
    deptIdFormat(row) {
      for (const item of this.deptList) {
        if (item.deptId == row.deptId) {
          return item.deptName;
        }
      }
      return row.deptId;
    },
     userIdFormat(row){
      return this.userFormat(row.userId)
    },
    // 人员翻译
    userFormat(id){
      for (const item of this.userList) {
        if (item.dictValue == id) {
          return item.dictLabel;
        }
      }
      return id;
    },

  }
};
</script>
<style lang="scss" scoped>
.app-container{
  //background: #f5f7f9;
  ::v-deep .el-card__header{
    padding: 0;
    line-height: 60px;
  }
  .header_btn{
    display: inline-block;
    margin-top: 3px;
    //float: right;
  }
}
.modal_form {
  .modal_form_item{
    width: 420px;
    display: inline-block
  }
  .modal_form_items{
    width: 470px;
  }
  ::v-deep .el-input__inner {
    font-size: 13px;
  }
  ::v-deep .el-textarea__inner {
    height: 120px;
  }
}
@media screen and (max-width:1480px) {
  ::v-deep .el-dialog{
    left: 130px;
  }
}
</style>
